// 这个模块 
// 定义全局变量
// 初始化父类 Square
// 通过父类初始化构造函数


// 定义全局变量
const rowNum = 30; // 行
const colNum = 30; // 列
const squareWidth = 20; // 每一squareDom的宽度
const eventBus = new Event();
const time = 200;
// 初始化父类 Square

/**
 * 
 * @param {*} x 列坐标
 * @param {*} y 行坐标
 * @param {*} type 方块类型 
 * @param {*} dom 对应的真实DOM
 */
function Square (x,y,type,color,dom) {
    this.x = x;
    this.y = y;
    this.type = type;
    this.color = color;
    this.realDom = dom || document.createElement('div'); 
}

Square.prototype.initDomStyle = function () {
    this.realDom.style.position = "absolute";
    this.realDom.style.left = this.x * squareWidth + 'px';
    this.realDom.style.top = this.y * squareWidth + 'px';
    this.realDom.style.width = squareWidth + 'px';
    this.realDom.style.height = squareWidth + 'px';
    this.realDom.style.background = this.color;
}

Square.prototype.appendDom = function (wrapper = gWrapper) {
    wrapper.appendChild(this.realDom);
}

Square.prototype.renderDom = function (wrapper) {
    this.initDomStyle();
    this.appendDom(wrapper);
}

Square.prototype.updateDom  =function () {
    this.initDomStyle();
}

Square.prototype.removeDom  =function () {
    this.realDom.remove();
}

// 通过父类初始化构造函数
var Ground = tools.singleton(Square);
var Wall = tools.extends(Square);
var Floor = tools.extends(Square);
var Food = tools.singleton(Square);
var SnakeHead = tools.singleton(Square);
var SnakeBody = tools.extends(Square);
var Snake = tools.singleton();
var Game = tools.singleton();
var Score = tools.singleton(Square);

// 初始化背景
var g = new Ground(3,2,'ground',"#abc");
Ground.prototype.initDomStyle = function () {
    this.realDom.style.position = "absolute";
    this.realDom.style.left = this.x * squareWidth  + 'px';
    this.realDom.style.top =  this.y * squareWidth  + 'px';
    this.realDom.style.width = rowNum * squareWidth + 'px';
    this.realDom.style.height = colNum * squareWidth + 'px';
    this.realDom.style.background = this.color;
}
Score.prototype.appendDom = Ground.prototype.appendDom = function () {
    document.body.appendChild(this.realDom);
}
g.initDomStyle();
g.appendDom();

var gWrapper = g.realDom;

// 初始化计分
Score.prototype.addScore = function () {
    this.n += 100;
    this.realDom.innerText = this.n + '分';
}
Score.prototype.init = function () {
    this.renderDom();
    this.n = 0;
    this.realDom.innerText = this.n + '分';
}

Score.prototype.initDomStyle = function () {
    this.realDom.style.position = "absolute";
    this.realDom.style.left = this.x * squareWidth  + 'px';
    this.realDom.style.top =  this.y * squareWidth  + 'px';
    this.realDom.style.whiteSpace = "nowrap";
    this.realDom.style.background = this.color;
    this.realDom.style.color = 'red';
}

var s = new Score(33,32,"score","#fff");


s.init();